package com.dicsystems.myrec;

import java.util.Calendar;
import java.util.List;

import com.dicsystems.myrec.dao.HolidayDao;
import com.dicsystems.myrec.dao.TodoDao;
import com.dicsystems.myrec.dto.Holiday;
import com.dicsystems.myrec.dto.Todo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CalendarActivity extends Activity {

	private Context me = this;
	private Common co = new Common(me);
	private TodoDao td = new TodoDao(me);
	private HolidayDao hd = new HolidayDao(me);
	private TextView year;
	private TextView month;
	private TableLayout calendar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar);
		year = (TextView)findViewById(R.id.year);
		month = (TextView)findViewById(R.id.month);
		calendar = (TableLayout)findViewById(R.id.calendar);
	}

	@Override
	protected void onResume() {
		super.onResume();
		td.open();
		hd.open();
		setCalendar(co.year(), co.month());
	}

	@Override
	protected void onPause() {
		super.onPause();
		td.close();
		hd.close();
	}

	private void setCalendar(int year, int month) {
		Calendar start_date = Calendar.getInstance();
		Calendar end_date = Calendar.getInstance();
		start_date.set(year, month - 1, 1, 0, 0, 0);
		end_date.set(year, month - 1, start_date.getActualMaximum(Calendar.DAY_OF_MONTH), 0, 0, 0);
		start_date.add(Calendar.DAY_OF_MONTH, - start_date.get(Calendar.DAY_OF_WEEK) + 1);
		end_date.add(Calendar.DAY_OF_MONTH, - end_date.get(Calendar.DAY_OF_WEEK) + 7);
		List<Holiday> holidays = hd.select(start_date, end_date);
		List<Todo> todos = td.select(start_date, end_date);
		this.year.setText(Integer.toString(year));
		if(month < 10) {
			this.month.setText("0" + Integer.toString(month));
		}
		else {
			this.month.setText(Integer.toString(month));
		}
		calendar.removeAllViews();
		TableRow row = new TableRow(this);
		row.addView(getWeek(R.string.sunday, R.color.red));
		row.addView(getWeek(R.string.monday, R.color.white));
		row.addView(getWeek(R.string.tuesday, R.color.white));
		row.addView(getWeek(R.string.wednesday, R.color.white));
		row.addView(getWeek(R.string.thursday, R.color.white));
		row.addView(getWeek(R.string.friday, R.color.white));
		row.addView(getWeek(R.string.saturday, R.color.blue));
		calendar.addView(row);
		while(start_date.compareTo(end_date) < 0) {
			TableRow row2 = new TableRow(this);
			for(int i = 0; i < 7; i++){
				View cell = getLayoutInflater().inflate(R.layout.calendar_day, null);
				Button day = (Button)cell.findViewById(R.id.day);
				cell.setId(Integer.parseInt(co.date(start_date).replaceAll("-", "")));
				day.setText(Integer.toString(start_date.get(Calendar.DAY_OF_MONTH)));
				if(start_date.get(Calendar.MONTH) + 1 == month) {
					switch(start_date.get(Calendar.DAY_OF_WEEK)) {
					case Calendar.SUNDAY:
						day.setTextColor(getResources().getColor(R.color.red));
						break;
					case Calendar.SATURDAY:
						day.setTextColor(getResources().getColor(R.color.blue));
						break;
					}
				}
				else {
					day.setTextColor(getResources().getColor(R.color.gray));
				}
				day.setTag(start_date.clone());
				day.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						Intent intent = new Intent(CalendarActivity.this, DailyListActivity.class);
						intent.putExtra("date", (Calendar)v.getTag());
						startActivity(intent);
					}
				});
				row2.addView(cell);
				start_date.add(Calendar.DAY_OF_MONTH, 1);
			}
			calendar.addView(row2);
		}
		setHoliday(holidays);
		setTodo(todos);
		setToday();
	}

	private View getWeek(int week, int color) {
		View cell = getLayoutInflater().inflate(R.layout.calendar_week, null);
		TextView week2 = (TextView)cell.findViewById(R.id.week);
		week2.setText(week);
		week2.setTextColor(getResources().getColor(color));
		return cell;
	}

	private void setHoliday(List<Holiday> holidays) {
		for(int i = 0; i < holidays.size(); i++) {
			View cell = findViewById(Integer.parseInt(holidays.get(i).id.replaceAll("-", "")));
			TextView day = (TextView)cell.findViewById(R.id.day);
			day.setTextColor(getResources().getColor(R.color.red));
		}
	}

	private void setTodo(List<Todo> todos) {
		for(int i = 0; i < todos.size(); i++) {
			View cell = findViewById(Integer.parseInt(todos.get(i).start_date.replaceAll("-", "")));
			TextView todo = (TextView)cell.findViewById(R.id.todo);
			todo.setVisibility(View.VISIBLE);
		}
	}

	private void setToday() {
		View cell = findViewById(Integer.parseInt(co.date().replaceAll("-", "")));
		if(cell != null) {
			TextView day = (TextView)cell.findViewById(R.id.day);
			cell.setBackgroundColor(getResources().getColor(R.color.aqua));
			day.setTextColor(getResources().getColor(R.color.white));
		}
	}

	public void setLastMonth(View view) {
		int year2 = Integer.parseInt(co.text(year));
		int month2 = Integer.parseInt(co.text(month));
		if(month2 > 1) {
			setCalendar(year2, month2 - 1);
		}
		else {
			setCalendar(year2 - 1, 12);
		}
	}

	public void setNextMonth(View view) {
		int year2 = Integer.parseInt(co.text(year));
		int month2 = Integer.parseInt(co.text(month));
		if(month2 < 12) {
			setCalendar(year2, month2 + 1);
		}
		else {
			setCalendar(year2 + 1, 1);
		}
	}

	public void setThisMonth(View view) {
		setCalendar(co.year(), co.month());
	}

	public void close(View view) {
		System.exit(RESULT_OK);
	}
}
